package bb.lanxing.util.ui;

import android.os.Handler;
import android.os.Looper;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import bb.lanxing.adapter.HeaderAdapterWrapper;
import bb.lanxing.adapter.IRVHeaderAdapter;
import bb.lanxing.view.LoadingView;
import java.lang.ref.WeakReference;

public class RecyclerViewLoadingHelper extends RecyclerView.OnScrollListener {
    private Handler handler;
    private ILoadableView mCallback;
    private IRVHeaderAdapter mHeaderAdapter;
    private LoadingView mLoadingView;
    private OnScrollListener onScrollListener;
    private WeakReference<RecyclerView> recyclerViewWeakReference;
    private int state;

    private boolean mEnabled = true;

    public interface OnScrollListener {
        void onScrolled(RecyclerView recyclerView, int dx, int dy);
    }

    public RecyclerViewLoadingHelper(ILoadableView callback) {
        this.mCallback = callback;
    }

    public boolean shouldShowLoading() {
        int i = this.state;
        return 1 == i || i == 2;
    }

    public boolean canBeLoading() {
        return this.state != 1 && this.mEnabled;
    }

    @Override
    public void onScrolled(@NonNull final RecyclerView recyclerView, int dx, int dy) {
        if (shouldBeLoading(recyclerView, dx, dy)) {
            loading(recyclerView);
        }
        OnScrollListener onScrollListener = this.onScrollListener;
        if (onScrollListener != null) {
            onScrollListener.onScrolled(recyclerView, dx, dy);
        }
    }

    @Override
    public void onScrollStateChanged(@NonNull final RecyclerView recyclerView, int newState) {
        if (newState == 0 && shouldBeLoading(recyclerView, 0, 0)) {
            loading(recyclerView);
        }
    }

    private void loading(RecyclerView recyclerView) {
        this.state = 1;
        if (this.mLoadingView == null) {
            this.mLoadingView = LoadingView.newInstanceFromLayout(recyclerView);
        }
        this.mLoadingView.loading();
        recyclerView.post(() -> {
            if (RecyclerViewLoadingHelper.this.state == 1) {
                RecyclerViewLoadingHelper.this.mHeaderAdapter.addFooterView(RecyclerViewLoadingHelper.this.mLoadingView);
            }
        });
        this.mCallback.onLoading();
    }

    public void setEnabled(boolean enabled) {
        this.mEnabled = enabled;
    }

    protected boolean shouldBeLoading(RecyclerView recyclerView, int dx, int dy) {
        LinearLayoutManager linearLayoutManager;
        int findLastVisibleItemPosition;
        RecyclerView.ViewHolder findViewHolderForAdapterPosition;
        if (!canBeLoading() || dy < 0 || (findLastVisibleItemPosition = (linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition()) == -1 || findLastVisibleItemPosition != linearLayoutManager.getItemCount() - 1 || (findViewHolderForAdapterPosition = recyclerView.findViewHolderForAdapterPosition(findLastVisibleItemPosition)) == null) {
            return false;
        }
        if (this.mLoadingView != null) {
            View view = findViewHolderForAdapterPosition.itemView;
            LoadingView loadingView = this.mLoadingView;
            if (view == loadingView || loadingView.getParent() != null) {
                return false;
            }
        }
        int height = (recyclerView.getHeight() - recyclerView.getPaddingBottom()) - (findViewHolderForAdapterPosition.itemView.getBottom() + recyclerView.getLayoutManager().getBottomDecorationHeight(findViewHolderForAdapterPosition.itemView));
        return height >= 0 && height < 5;
    }

    public void failure() {
        if (hasLoadingView()) {
            this.state = 2;
            this.mLoadingView.failure();
            this.mHeaderAdapter.removeFooterView(this.mLoadingView);
        }
    }

    public void complete() {
        if (hasLoadingView()) {
            this.mHeaderAdapter.removeFooterView(this.mLoadingView);
            Handler handler = this.handler;
            if (handler != null) {
                handler.post(() -> RecyclerViewLoadingHelper.this.state = 3);
            } else {
                this.state = 3;
            }
        }
    }

    private boolean hasLoadingView() {
        return shouldShowLoading();
    }

    public void attachToRecyclerView(RecyclerView recyclerView) {
        this.recyclerViewWeakReference = new WeakReference<>(recyclerView);
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if (adapter instanceof IRVHeaderAdapter) {
            this.mHeaderAdapter = (IRVHeaderAdapter) adapter;
            recyclerView.removeOnScrollListener(this);
            recyclerView.addOnScrollListener(this);
            if (this.handler == null) {
                this.handler = new Handler(Looper.getMainLooper());
            }
        }
    }

    public static RecyclerView.Adapter wrap(RecyclerView.Adapter adapter) {
        if (adapter instanceof IRVHeaderAdapter) {
            return adapter;
        } else {
            return new HeaderAdapterWrapper(adapter).getWrappedAdapter();
        }
    }

    public void detachFromRecyclerView(RecyclerView recyclerView) {
        Handler handler = this.handler;
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
        this.mHeaderAdapter = null;
        this.mCallback = null;
        this.handler = null;
        recyclerView.removeOnScrollListener(this);
    }
}
